#include "message.hpp"
#include "net.hpp"
#include "dispatcher.hpp"
#include <thread>
#include <iostream>

void Test1()
{
    // zl::RpcRequest::ptr t1 = std::dynamic_pointer_cast<zl::RpcRequest>(zl::MessageFactory::create(zl::MType::REQ_RPC));
    // t1->setMethod("test");
    // Json::Value val;
    // val["name"] = "zl";
    // val["age"] = 18;
    // t1->setParams(val);
    // std::string ret = t1->serialize();
    // std::cout << ret << std::endl;

    // zl::RpcRequest::ptr t2 = zl::MessageFactory::create<zl::RpcRequest>();
    // bool ret2 = t2->unserialize(ret);
    // if(ret2==false)
    // return -1;
    // ret2=t2->check();
    // if(ret2==false)
    // return -1;

    // std::cout << t2->method() << std::endl;
    // std::cout<<t2->params()["age"].asInt()<<std::endl;
    // std::cout<<t2->params()["name"].asString()<<std::endl;

    //  zl::TopicRequest::ptr t2 = zl::MessageFactory::create<zl::TopicRequest>();
    //  t2->setTopicKey("test");
    //  t2->setOptype(zl::TopicOptype::TOPIC_PUBLISH);
    //  t2->setTopicMsg("lll11111");
    //  std::string ret=t2->serialize();
    //  std::cout<<ret<<std::endl;

    //  zl::TopicRequest::ptr t3 = zl::MessageFactory::create<zl::TopicRequest>();
    // bool ret2 = t3->unserialize(ret);
    // if(ret2==false)
    // return -1;
    // ret2=t3->check();
    // if(ret2==false)
    // return -1;

    // std::cout << t3->topicKey() << std::endl;
    // std::cout<<(int)t3->optype()<<std::endl;
    // std::cout<<t3->topicMsg()<<std::endl;

    //   zl::RpcResponse::ptr t2 = std::dynamic_pointer_cast<zl::RpcResponse>(zl::MessageFactory::create(zl::MType::RSP_RPC));
    //   t2->setRCode(zl::RCode::RECODE_OK);
    //  t2->setresult(11);
    //   std::string ret = t2->serialize();
    //   std::cout << ret << std::endl;

    //    zl::RpcResponse::ptr t3 = zl::MessageFactory::create<zl::RpcResponse>();
    //   bool ret2 = t3->unserialize(ret);
    //   if(ret2==false)
    //   return -1;
    //   ret2=t3->check();
    //   if(ret2==false)
    //   return -1;

    //   std::cout <<(int) t3->rcode()<< std::endl;
    //   std::cout<<t3->result().asInt()<<std::endl;

    // zl::TopicResponse::ptr t2 = std::dynamic_pointer_cast<zl::TopicResponse>(zl::MessageFactory::create(zl::MType::RSP_TOPIC));
    // t2->setRCode(zl::RCode::RECODE_OK);
    // std::string ret = t2->serialize();
    // std::cout << ret << std::endl;

    // zl::TopicResponse::ptr t3 = zl::MessageFactory::create<zl::TopicResponse>();
    // bool ret2 = t3->unserialize(ret);
    // if (ret2 == false)
    //     return -1;
    // ret2 = t3->check();
    // if (ret2 == false)
    //     return -1;

    // std::cout << (int)t3->rcode() << std::endl;

    // zl::ServiceResponse::ptr t2 = std::dynamic_pointer_cast<zl::ServiceResponse>(zl::MessageFactory::create(zl::MType::RSP_SERVICE));
    // t2->setRCode(zl::RCode::RECODE_OK);
    // t2->setMethod("test");
    // t2->setOptype(zl::ServiceOptype::SERVICE_DISCOVERY);
    // std::vector<zl::Address> addrs;
    // addrs.push_back(zl::Address("127.0.0.1", 8080));
    // addrs.push_back(zl::Address("127.0.0.1", 8081));
    // t2->setHost(addrs);

    // std::string ret = t2->serialize();
    // std::cout << ret << std::endl;

    // zl::ServiceResponse::ptr t3 = zl::MessageFactory::create<zl::ServiceResponse>();
    // bool ret2 = t3->unserialize(ret);
    // if (ret2 == false)
    //     return -1;
    // ret2 = t3->check();
    // if (ret2 == false)
    //     return -1;

    // std::cout << (int)t3->rcode() << std::endl;
    // std::cout << (int)t3->optype() << std::endl;
    // std::cout << t3->method() << std::endl;
    // std::vector<zl::Address> hosts = t3->hosts();
    // for (auto &host : hosts)
    // {
    //     std::cout << host.first << ":" << host.second << std::endl;
    // }
}

using namespace zl;

void onRpcResponse(const zl::BaseConnection::ptr &conn, zl::RpcResponse::ptr &msg)
{
    std::cout << "收到Rpc响应\n";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}

void onTopicResponse(const zl::BaseConnection::ptr &conn, zl::TopicResponse::ptr &msg)
{
    std::cout << "收到Topic响应\n";
    std::string body = msg->serialize();
    std::cout << body << std::endl;
}

int main()
{
    auto dispatcherr = std::make_shared<zl::Dispatcher>();
    dispatcherr->registerHandler<zl::RpcResponse>(zl::MType::RSP_RPC, onRpcResponse);
    dispatcherr->registerHandler<zl::TopicResponse>(zl::MType::RSP_TOPIC, onTopicResponse);

    auto client = zl::ClientlFactory::create("127.0.0.1", 8080);
    auto message_cb = std::bind(&zl::Dispatcher::onMessage, dispatcherr.get(), std::placeholders::_1, std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();

    // 发送 RPC 请求
    auto rpc_req = zl::MessageFactory::create<zl::RpcRequest>();
    rpc_req->setMethod("test");
    rpc_req->setId("123");
    rpc_req->setMType(zl::MType::REQ_RPC);
    Json::Value val;
    val["name"] = "zl";
    val["age"] = 18;
    rpc_req->setParams(val);
    client->send(rpc_req);

    // 发送主题请求
    auto topic_req = zl::MessageFactory::create<zl::TopicRequest>();

    topic_req->setId("1232222");
    topic_req->setMType(zl::MType::REQ_TOPIC);

    client->send(topic_req);

    // 等待 10 秒，确保消息发送完成
    std::this_thread::sleep_for(std::chrono::seconds(10));
    client->shutdown();

    return 0;
}